import os
from extract import *

# 处理数据，利用 javaparser 来将 源 path 目标 给挑出来

TRAIN_DIR = r"F:\博士\看的论文\data\precode2vec\Java\ProjectEuler"
VAL_DIR = r"F:\博士\看的论文\data\precode2vec\Java\ProjectEuler"
TEST_DIR = r"F:\博士\看的论文\data\precode2vec\Java\ProjectEuler"
DATASET_NAME = "javaFirst"
MAX_CONTEXTS = 200
WORD_VOCAB_SIZE = 1301136
PATH_VOCAB_SIZE = 911417
TARGET_VOCAB_SIZE = 261245
NUM_THREADS = 64

TRAIN_DATA_FILE = DATASET_NAME + ".train.raw.txt"
VAL_DATA_FILE = DATASET_NAME + ".val.raw.txt"
TEST_DATA_FILE = DATASET_NAME + ".test.raw.txt"
EXTRACTOR_JAR = r"JavaExtractor/JPredict/target/JavaExtractor-0.0.1-SNAPSHOT.jar"

try:
    os.mkdir(r"./data")
    os.mkdir(r"./data/" + DATASET_NAME)
except:
    print("already have data directory")

TARGET_HISTOGRAM_FILE = "data/" + DATASET_NAME + ".histo.tgt.c2v"
ORIGIN_HISTOGRAM_FILE = "data/" + DATASET_NAME + ".histo.ori.c2v"
PATH_HISTOGRAM_FILE = "data/" + DATASET_NAME + ".histo.path.c2v"


def han():
    # #############################     1     ############################################################ cat  ${
    # TRAIN_DATA_FILE} | cut -d' ' -f1 | awk '{n[$0]++} END {for (i in n) print i,n[i]}' > ${TARGET_HISTOGRAM_FILE}
    _dict = {}
    with open("my.train.raw.txt", "r") as file:
        for line in file:
            f1 = line.split(' ')[0]
            if _dict.__contains__(f1):
                _dict[f1] += 1
            else:
                _dict[f1] = 0

    with open(TARGET_HISTOGRAM_FILE, 'w') as file:
        for key, value in _dict.items():
            file.write(key + ',' + str(value) + '\n')

    # #############################     2  3  ############################################################ cat ${
    # TRAIN_DATA_FILE} | cut -d' ' -f2- | tr ' ' '\n' | cut -d',' -f1,3 | tr ',' '\n' | awk '{n[$0]++} END {for (i in
    # n) print i,n[i]}' > ${ORIGIN_HISTOGRAM_FILE} cat ${TRAIN_DATA_FILE} | cut -d' ' -f2- | tr ' ' '\n' | cut -d',
    # ' -f2 | awk '{n[$0]++} END {for (i in n) print i,n[i]}' > ${PATH_HISTOGRAM_FILE}

    ls = []  # 第 2 步
    dict1 = {}
    ls3 = []  # 第 3 步
    dict3 = {}
    with open("my.train.raw.txt", "r") as file:
        for line in file:
            f1 = line.split(' ')[1:]
            f1[-1] = f1[-1].replace('\n', '')
            for s in f1:
                t = s.split(',')
                ls.append(t[0])
                ls.append(t[2])
                ls3.append(t[1])

        for i in ls:
            if dict1.__contains__(i):
                dict1[i] += 1
            else:
                dict1[i] = 0

        for i in ls3:
            if dict1.__contains__(i):
                dict3[i] += 1
            else:
                dict3[i] = 0

    with open(ORIGIN_HISTOGRAM_FILE, 'w') as file:
        for key, value in dict1.items():
            file.write(key + ',' + str(value) + '\n')

    with open(PATH_HISTOGRAM_FILE, 'w') as file:
        for key, value in dict3.items():
            file.write(key + ',' + str(value) + '\n')


if __name__ == '__main__':
    # "Extracting paths from validation set..."
    extract_parse = parse_init()
    extract_args = extract_parse.parse_args(
        ["--jar", EXTRACTOR_JAR, "--dir", VAL_DIR, "--max_path_length", "8", "--max_path_width", "2", "--num_threads",
         "64"])
    extract_run(extract_args)
    # "Finished extracting paths from validation set"
    han()
